/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SocketBusiness;

import CommonValue.SystemTimer;
import DTO.ControlDTO;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class SlaveObeyBoss extends ServerListen {

    public SlaveObeyBoss(String systemSign, int port, ControlDTO operation) {
        super(systemSign, port, operation);
    }

    @Override
    public void business() {
        boolean finish = false;

        do {
            try {
                Thread.sleep(SystemTimer.CheckReportSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(SlaveObeyBoss.class.getName()).log(Level.SEVERE, null, ex);
            }
            Object obj = new Object();
            try {
                obj = this.receiveMessage();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SlaveObeyBoss.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (obj == null) {
                System.out.println("controlDTO is null");
                return;
            }
            
            if (!obj.getClass().equals(ControlDTO.class)) {
                System.out.println("Invalid Type Class");
                return;
            }

            synchronized (this.getOperation()) {
                this.getOperation().setFlag(((ControlDTO) obj).getFlag());
            }
            System.out.println("DA NHAN TIN HIEU " + (this.getOperation().getFlag() ? "ON" : "OFF") + " TU HE THONG " + this.getOperation().getBoss() + "TOI 1 HE THONG SLAVE : " + this.getOperation().getSlave());

        } while (!finish);
    }
}
